class People {
    name?: string;
    age: number;
    addr:string;
    constructor(_name:string,_age:number,_addr:string){
        // this指向创建实例的对象
        this.name = _name
        this.age = _age
        this.addr = _addr
    }
    doEat(who:string,where:string){
        console.log(`who:${who},where:${where}`);
    }
    doStep(){}
}

class StringUtil{
    static trimSpace(str:string){
        return str.replace(/\s+/g,"")
    }
}

const dataProp1 = Object.getOwnPropertyDescriptor(People.prototype,"doEat")
const targetMethod=dataProp1!.value;
// console.log('dataProp1',dataProp1);
// const dataProp2 = Object.getOwnPropertyDescriptor(People.prototype,"doEat")
// console.log('dataProp2',dataProp2);
// console.log(dataProp1 == dataProp2); // false
// dataProp1?.value()  // 这是原来的方法
dataProp1!.value = function(...args:any[]){ // 这是指向新的方法
    args = args.map(arg=>{
        if(typeof arg === 'string') return StringUtil.trimSpace(arg)
            return arg
    })
    console.log("前置拦截");
        // console.log("args", args);
        targetMethod.apply(this,args)
        console.log("this", this); // this 指向 dataProp1
    console.log("后置拦截");
} 
dataProp1?.value("慧杰","楷洁")
// 定义属性
Object.defineProperty(People.prototype,"doEat",dataProp1!)

let p = new People("peter",23,"gongzhu")
p.doEat("李  四 ","王  五 ")

export {}
